package com.epam.edu.tags;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;

public class DecorBox extends TagSupport {
    private String align;
    private String title;
    private String titleColor;
    private String titleAlign;
    private String color;
    private String borderColor;
    private String margin;
    private String borderWidth;

    @Override
    public int doStartTag() throws JspException {
        try {
            JspWriter out = pageContext.getOut();
            out.print("<div align='" + align + "'>" +
                    "<table bgcolor='" + borderColor +
                    "' border='0' cellspacing='0' cellpadding='" + borderWidth + "'");
            if (title != null) {
                out.print("<tr><td align='" + titleAlign + "'>" +
                        "<font face='helvetika' size='+1' color='" + titleColor + "'><b>"
                        + title + "</b></font></td></tr>");
            }
            out.print("<tr><td><table bgcolor='" + color +
                    "' border='0' cellspacing='0' cellpadding='" + margin + "'<tr><td>");
        } catch (IOException exception) {
            throw new JspException(exception.getMessage());
        }
        return EVAL_BODY_INCLUDE;
    }

    @Override
    public int doEndTag() throws JspException {
        try {
            JspWriter out = pageContext.getOut();
            out.print("</td></tr></table></td></tr></table></div>");
        } catch (IOException exception) {
            throw new JspException(exception.getMessage());
        }
        return EVAL_PAGE;
    }

    @Override
    public void setPageContext(PageContext pageContext) {
        super.setPageContext(pageContext);
        align = "center";
        title = null;
        titleColor = "white";
        titleAlign = "left";
        color = "white";
        borderColor = "#2c6932";
        margin = "20";
        borderWidth = "4";
    }

    public void setAlign(String align) {
        this.align = align;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setTitleColor(String titleColor) {
        this.titleColor = titleColor;
    }

    public void setTitleAlign(String titleAlign) {
        this.titleAlign = titleAlign;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public void setBorderColor(String borderColor) {
        this.borderColor = borderColor;
    }

    public void setMargin(String margin) {
        this.margin = margin;
    }

    public void setBorderWidth(String borderWidth) {
        this.borderWidth = borderWidth;
    }

}
